/**
 * 初始化字典详情对话框
 */
var DictInfoDlg = {
    count: $("#itemSize").val(),
    dictName: '',			//字典的名称
    mutiString: '',		//拼接字符串内容(拼接字典条目)
    itemTemplate: $("#itemTemplate").html(),
    DictInfoData :{}
};

/**
 * 清空数据
 */
DictInfoDlg.clearData = function () {
    this.DictInfoData = {}
}


/**
 * 只允许input输入数字和小数点
 * @param value
 * @param e
 * @returns {boolean}
 */
DictInfoDlg.isNum = function(obj) {

    if($("#proportion").val() != '' && $("#proportion").val() != 0){
        $("#amount").attr("disabled","disabled");
    }else{
        $("#amount").removeAttr("disabled");
    }

    if($("#amount").val() != ''  && $("#amount").val() != 0){
        $("#proportion").attr("disabled","disabled");
    }else{
        $("#proportion").removeAttr("disabled","");
    }

    if($("#multiple").val() != ''  && $("#multiple").val() != 0){
        $("#maxValue").attr("disabled","disabled");
    }else{
        $("#maxValue").removeAttr("disabled");
    }

    if($("#maxValue").val() != ''  && $("#maxValue").val() != 0){
        $("#multiple").attr("disabled","disabled");
    }else{
        $("#multiple").removeAttr("disabled","");
    }
    if(obj != undefined) {
        obj.value = obj.value.replace(/[^\d.]/g, "");  //清除“数字”和“.”以外的字符
        obj.value = obj.value.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的
        obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
        obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d{4}).*$/, '$1$2.$3');//只能输入四位小数
        if (obj.value.indexOf(".") < 0 && obj.value != "") {//以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额
            obj.value = parseFloat(obj.value);
        }
    }
}

/**
 * item获取新的id
 */
DictInfoDlg.newId = function () {
    if(this.count == undefined){
        this.count = 0;
    }
    this.count = this.count + 1;
    return "dictItem" + this.count;
};

/**
 * 关闭此对话框
 */
DictInfoDlg.close = function () {
    parent.layer.close(window.parent.Dict.layerIndex);
};

/**
 * 添加条目
 */
DictInfoDlg.addItem = function () {
    $("#itemsArea").append(this.itemTemplate);
    $("#dictItem").attr("id", this.newId());
};

/**
 * 删除item
 */
DictInfoDlg.deleteItem = function (event) {
    var obj = Feng.eventParseObject(event);
    obj.parent().parent().remove();
};

/**
 * 清除为空的item Dom
 */
DictInfoDlg.clearNullDom = function(){
    $("[name='dictItem']").each(function(){
        var num = $(this).find("[name='itemNum']").val();
        var name = $(this).find("[name='itemName']").val();
        if(num == '' || name == ''){
            $(this).remove();
        }
    });
};

/**
 * 收集添加字典的数据
 */
DictInfoDlg.collectData = function () {
    this.clearNullDom();
    $("[name='dictItem']").each(function(){
        var num = $(this).find("[name='num']").val();
        var name = $(this).find("[name='name']").val();
        var tips = $(this).find("[name='tips']").val();
        var pidname = $(this).find("[name='pidname']").val();
    });
    this.id = $("#id").val();
    this.num = $("#num").val();
    this.name = $("#name").val();
    this.tips = $("#tips").val();
    this.pidname = $("#pidname").val();
    this.proportion = $("#proportion").val();
    this.amount = $("#amount").val();
    this.multiple = $("#multiple").val();
    this.maxValue = $("#maxValue").val();
};


/**
 * 提交添加字典
 */
DictInfoDlg.addSubmit = function () {
    this.collectData();

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/dict/add", function (data) {
        Feng.success("添加成功!");
        window.parent.Dict.table.refresh();
        DictInfoDlg.close();
    }, function (data) {
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set('pidname',this.pidname);
    ajax.set('name',this.name);
    ajax.set('tips',this.tips);
    ajax.set('num',this.num);
    ajax.start();
};



/**
 * 显示字典父级菜单的树添加页面使用
 *
 * @returns
 */
DictInfoDlg.searchUserByType = function () {
    Feng.showInputTree("pidname", "dictContent");
    $("#pidname").prop("readonly",true).typeahead("lock",true);

};

DictInfoDlg.onClickPName = function (e, treeId, treeNode) {
    $("#pidname").val( DictInfoDlg.areaZtree.getSelectedVal());
    $("#receiveTip").val(treeNode.id);
};


DictInfoDlg.HTMLDOM = function (id,name,val) {
    var div = $("<div></div>").addClass("form-group").css("height","34px").css("lineHeight","34px");
    var label = $("<label></label>").addClass("col-sm-2").addClass("control-label").css("height","34px")
        .css("lineHeight","34px").css("textAlign","center").css("alignContent","center").css("width","70px")
        .css("padding","5px").text(name);
    var divInp = $("<div></div>").addClass("col-sm-9").css("padding","5px");
    var input = $("<input />").addClass("form-control").attr("id",id).attr("onkeyup","DictInfoDlg.isNum(this)").attr("type","type").attr("name",id).val(val);
    divInp.append(input);
    div.append(label).append(divInp);
    var divHr = $("<div></div>").addClass("hr-line-dashed");
    div.appendTo("#dictDiv");
    divHr.appendTo("#dictDiv");
}



/**
 * 提交修改
 */
DictInfoDlg.editSubmit = function () {
    this.clearData();
    this.collectData();
    var ajax = new $ax(Feng.ctxPath + "/dict/update", function (data) {
        Feng.success("修改成功!");
        window.parent.Dict.table.refresh();
        DictInfoDlg.close();
    }, function (data) {
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    this.DictInfoData.id = this.id;
    this.DictInfoData.num = this.num;
    this.DictInfoData.name = this.name;
    if($("#proportion").length != 0 && $("#amount").length != 0){
        this.DictInfoData.tips = "{\"proportion\":" + parseFloat(this.proportion)  + ",\"amount\":" + parseFloat(this.amount) + "}";
    }else if($("#multiple").length != 0 && $("#maxValue").length != 0){
        this.DictInfoData.tips = "{\"multiple\":" + parseFloat(this.multiple) + ",\"maxValue\":" + parseFloat(this.maxValue) + "}";
    } else{
        this.DictInfoData.tips = this.tips;
    }
    ajax.set(this.DictInfoData);
    ajax.start();
};


$(function() {
    //树
    var receiveTip = new $ZTree("dictTree", "/dict/dicttree",function(){
    });
    receiveTip.setReady( function (data) {
        data.map(function(e){
            e.open=false;
        })
    });
    receiveTip.bindOnClick(DictInfoDlg.onClickPName);
    receiveTip.init();
    DictInfoDlg.areaZtree = receiveTip;





    /**
     * 修改回显数据
     */
    if($("#id").val()){
        var ajax = new $ax(Feng.ctxPath + "/dict/dictById/" + $("#id").val(), function (data) {
            $("#name").val(data.name);
            if(Feng.isJSON(data.tips)){
                var obj = JSON.parse(data.tips);
                $.each(obj,function (index,item) {
                    if(index == "proportion"){
                        $("#tips").parent().parent().hide();
                        $(".tips").hide();
                        DictInfoDlg.HTMLDOM("proportion","比例",item);
                        $("input[id=proportion]").val(item);
                    }
                    if(index == "amount"){
                        $("#tips").parent().parent().hide();
                        $(".tips").hide();
                        DictInfoDlg.HTMLDOM("amount","金额",item);
                        $("input[id=amount]").val(item);
                    }
                    if(index == "multiple"){
                        $("#tips").parent().parent().hide();
                        $(".tips").hide();
                        DictInfoDlg.HTMLDOM("multiple","倍数",item);
                        $("input[id=multiple]").val(item);

                    }
                    if(index == "maxValue"){
                        $("#tips").parent().parent().hide();
                        $(".tips").hide();
                        DictInfoDlg.HTMLDOM("maxValue","最大值",item);
                        $("input[id=maxValue]").val(item);
                    }
                });
                DictInfoDlg.isNum();
            }else {
                $("#tips").val(data.tips);
            }
            $("#num").val(data.num);
        }, function (data) {
            Feng.error("数据显示失败!");
        });
        ajax.start();
    }
});






